<?php
class item_changedisplayname extends template_item
{
	var $default_name = "Change Your Display Name";
	var $default_description = "Change your your display name with this item. Changing display name with this item will bypass the limited changes you can make.";
	
	var $allow_multiple = 1;
	
	function admin_form()
	{
		if(!$this->ipsclass->vars['auth_allow_dnames'])
		{
			$this->ipsclass->admin->error("ITEM ERROR: You must enable display names to enable this item. If you do not want to enable display names, use 'Change Your Login Name' item.");
		}
	}
	
	function admin_save()
	{
		if(!$this->ipsclass->vars['auth_allow_dnames'])
		{
			$this->ipsclass->admin->error("ITEM ERROR: You must enable display names to enable this item. If you do not want to enable display names, use 'Change Your Login Name' item.");
		}
	}
	
	function public_use_form($inventory_ids = array())
	{
		$this->ipsclass->load_language('lang_market_items');
		
		if(!$this->ipsclass->vars['auth_allow_dnames'])
		{
			$this->ipsclass->Error(array('MSG'=>'display_name_disabled'));
		}
		
		$hidden_fields = $this->_get_hidden_fields($inventory_ids);
		
		return <<<EOF
<div class='formsubtitle'>{$this->ipsclass->lang['change_own_display']}</div>
<form action='{$this->base_url}' method='post'>
{$hidden_fields}
	<table class='ipbtable' cellspacing='0' cellpadding='0'>
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['change_own_display_to']}</b>
			</td>
			<td class='row2' align='right'>
				<input type='input' name='display_name' value='{$this->ipsclass->member['members_display_name']}' size='30' class='forminput' />
			</td>
		</tr>
		<tr>
			<td class='darkrow1' align='center' colspan='2'>
				<input type='submit' name='item_used' value='{$this->ipsclass->lang['save_info']}' class='forminput' />
			</td>
		</tr>
	</table>
</form>
EOF;
	}
	
	function input_done()
	{
		return isset($this->ipsclass->input['display_name']) && isset($this->ipsclass->input['item_used']);
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		if(!$this->ipsclass->vars['auth_allow_dnames'])
		{
			$this->ipsclass->Error(array('MSG'=>'display_name_disabled'));
		}
		
		$display_name = trim( $this->ipsclass->input['display_name'] );
		$banfilters   = array();
		
		if ( $this->ipsclass->vars['strip_space_chr'] )
		{
			// Use hexdec to convert between '0xAD' and chr
			$member_name = str_replace( chr(160), ' ', $display_name );
			$member_name = str_replace( chr(173), ' ', $display_name );
			$member_name = str_replace( chr(240), ' ', $display_name );
			
			$member_name = trim($member_name);
		}
		
		$unicode_name = preg_replace_callback('/&#([0-9]+);/si', create_function( '$matches', 'return chr($matches[1]);' ), $display_name);
		$unicode_name  = str_replace( "'" , '&#39;', $unicode_name );
		$unicode_name  = str_replace( "\\", '&#92;', $unicode_name );
		
		$this->ipsclass->DB->simple_construct( array( 'select' => '*', 'from' => 'banfilters' ) );
		$this->ipsclass->DB->simple_exec();
		
		while( $r = $this->ipsclass->DB->fetch_row() )
		{
			$banfilters[ $r['ban_type'] ][] = $r['ban_content'];
		}
		
		if ( ! $display_name OR strlen( $display_name ) < 3 AND strlen( $display_name ) > $this->ipsclass->vars['max_user_name_length'] )
		{
			$this->ipsclass->Error(array('MSG'=>'display_name_error_length'));
		}
		
		if ( preg_match( "#[\[\];,\|]#",  str_replace('&#39;', "'", $unicode_name ) ) )
		{
			$this->ipsclass->Error(array('MSG'=>'display_name_error_unicode'));
		}
		
		if ( is_array( $banfilters['name'] ) and count( $banfilters['name'] ) )
		{
			foreach ( $banfilters['name'] as $n )
			{
				if ( $n == "" )
				{
					continue;
				}
				
				if ( preg_match( "/".preg_quote($n, '/' )."/i", $display_name ) )
				{
					$this->ipsclass->Error(array('MSG'=>'display_name_found'));
					break;
				}
			}
		}
		
		$this->ipsclass->DB->build_query( array( 'select' => "members_display_name, id",
												 'from'   => 'members',
												 'where'  => "members_l_display_name='".strtolower($display_name)."' AND id != ".$this->ipsclass->member['id'],
												 'limit'  => array( 0,1 ) ) );
		$this->ipsclass->DB->exec_query();
		if ( $this->ipsclass->DB->get_num_rows() )
 		{
			$this->ipsclass->Error(array('MSG'=>'display_name_found'));
		}
		
		if ( $this->ipsclass->vars['auth_dnames_nologinname'] )
		{
			$this->ipsclass->DB->build_query( array( 'select' => "members_display_name, id",
													 'from'   => 'members',
													 'where'  => "members_l_username='".strtolower($display_name)."' AND id != ".$this->ipsclass->member['id'],
													 'limit'  => array( 0,1 ) ) );
												 
			$this->ipsclass->DB->exec_query();
			
			if ( $this->ipsclass->DB->get_num_rows() )
			{
				$this->ipsclass->Error(array('MSG'=>'display_name_found'));
			}
		}
		
		if ( $unicode_name != $display_name )
		{
			$this->ipsclass->DB->build_query( array( 'select' => "members_display_name, id, email",
													 'from'   => 'members',
													 'where'  => "members_l_display_name='".strtolower($unicode_name)."' AND id != ".$this->ipsclass->member['id'],
													 'limit'  => array( 0,1 ) ) );
			$this->ipsclass->DB->exec_query();
			
			if ( $this->ipsclass->DB->get_num_rows() )
			{
				$this->ipsclass->Error(array('MSG'=>'display_name_found'));
			}
			
			if ( $this->ipsclass->vars['auth_dnames_nologinname'] )
			{
				$this->ipsclass->DB->build_query( array( 'select' => "members_display_name, id",
														 'from'   => 'members',
														 'where'  => "members_l_username='".strtolower($unicode_name)."' AND id != ".$this->ipsclass->member['id'],
														 'limit'  => array( 0,1 ) ) );
													 
				$this->ipsclass->DB->exec_query();
				
				if ( $this->ipsclass->DB->get_num_rows() )
				{
					$this->ipsclass->Error(array('MSG'=>'display_name_found'));
				}
			}
		}
		
		$this->ipsclass->DB->do_update('members',array(
														'members_display_name' => $display_name,
														'members_l_display_name' => strtolower( $display_name )
				),"id=".$this->ipsclass->member['id']);
		
		$this->ipsclass->DB->do_update( 'contacts'      , array( 'contact_name'     => $display_name ), "contact_id="    .$this->ipsclass->member['id'] );
		$this->ipsclass->DB->do_update( 'forums'        , array( 'last_poster_name' => $display_name ), "last_poster_id=".$this->ipsclass->member['id'] );
		$this->ipsclass->DB->do_update( 'sessions'      , array( 'member_name'      => $display_name ), "member_id="     .$this->ipsclass->member['id'] );
		$this->ipsclass->DB->do_update( 'topics'        , array( 'starter_name'     => $display_name ), "starter_id="    .$this->ipsclass->member['id'] );
		$this->ipsclass->DB->do_update( 'topics'        , array( 'last_poster_name' => $display_name ), "last_poster_id=".$this->ipsclass->member['id'] );
		
    	$this->ipsclass->DB->force_data_type = array( 'dname_previous' => 'string',
    												  'dname_current'  => 'string' );
    	
    	$this->ipsclass->DB->do_insert( 'dnames_change', array( 'dname_member_id'  => $this->ipsclass->member['id'],
    														    'dname_date'       => time(),
    														    'dname_ip_address' => $this->ipsclass->ip_address,
    														    'dname_previous'   => $this->ipsclass->member['members_display_name'],
    														    'dname_current'    => $display_name ) );
    	
		require_once( ROOT_PATH .'sources/action_admin/moderator.php' );
		$admod = new ad_moderator();
		$admod->ipsclass =& $this->ipsclass;
		
		$admod->rebuild_moderator_cache();
		
		require_once( ROOT_PATH.'sources/action_public/announcements.php' );
		$announcements = new announcements();
		$announcements->ipsclass =& $this->ipsclass;
		$announcements->announce_recache();
		
		$this->ipsclass->update_forum_cache();
		
		$this->ipsclass->lang['change_name_to_x'] = str_replace('<#NAME#>', $display_name, $this->ipsclass->lang['change_name_to_x']);
		
		$this->delete_items($inventory_ids);
		
		$inv_id = reset($inventory_ids);
		$this->_write_log($inv_id, "Changed display name from {$this->ipsclass->member['members_display_name']} to {$display_name}");
		
		$this->ipsclass->market_class->redirect('change_name_to_x');
	}
}
?>